package com.zsgeek.wisdchain.qutu.view.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.zsgeek.wisdchain.qutu.BaseActivity;
import com.zsgeek.wisdchain.qutu.R;
import com.zsgeek.wisdchain.qutu.base.customControl.ClearEditText;
import com.zsgeek.wisdchain.qutu.tools.TextUtils;
import com.zsgeek.wisdchain.qutu.tools.ToastHelper;

import butterknife.BindView;

/**
 * 设置连接
 */
public class SetConnectActivity extends BaseActivity {
    @BindView(R.id.content_et)
    ClearEditText contentEt;

    @Override
    public int getLayoutId() {
        return R.layout.activity_set_connect;
    }

    @Override
    protected void initView() {
        setTitle("设置连接");
        setToolbarRight(getString(R.string.save), null, 0, 0, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = contentEt.getText().toString().trim();
                if (TextUtils.isEmpty(content)) {
                    ToastHelper.showToast("请输入链接");
                    return;
                }
                if (isURL(content)) {
                    setResult(-1, new Intent().putExtra("URL", content));
                    finish();
                } else {
                    ToastHelper.showToast("请输入正确链接");
                }
            }
        });
    }

    @Override
    protected void initDate(Bundle savedInstanceState) {

    }

    /**
     *      * 判断一个字符串是否为url
     *      * @param str String 字符串
     *      * @return boolean 是否为url
     *      * @author peng1 chen
     *      *
     **/
    private boolean isURL(String str) {
        //转换为小写
        str = str.toLowerCase();
        String regex = "^((https|http|ftp|rtsp|mms)?://)"//https、http、ftp、rtsp、mms
                + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?"//ftp的user@  
                + "(([0-9]{1,3}\\.){3}[0-9]{1,3}" // IP形式的URL- 例如：199.194.52.184  
                + "|" // 允许IP和DOMAIN（域名）
                + "([0-9a-z_!~*'()-]+\\.)*" // 域名- www.  
                + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\." // 二级域名  
                + "[a-z]{2,6})" // first level domain- .com or .museum  
                + "(:[0-9]{1,5})?" // 端口号最大为65535,5位数
                + "((/?)|" // a slash isn't required if there is no file name  
                + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
        return str.matches(regex);
    }
}
